package com.dana.system.controller.exam;

import com.core.controller.BaseController;
import com.core.domain.Result;
import com.core.domain.TableDataInfo;
import com.dana.system.domain.exam.dto.ExamAddDTO;
import com.dana.system.domain.exam.dto.ExamAddQuestionDTO;
import com.dana.system.domain.exam.dto.ExamEditDTO;
import com.dana.system.domain.exam.dto.QueryExamDTO;
import com.dana.system.domain.exam.vo.ExamDetailVO;
import com.dana.system.service.exam.IExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController {
    @Autowired
    IExamService examService;

    /**
     * 获取题目列表
     * @param queryExamDTO
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(QueryExamDTO queryExamDTO){
        return getTableDataInfo(examService.getList(queryExamDTO));
    }

    /**
     * 新增竞赛无题目
     * @param examAddDTO
     * @return
     */

    @PostMapping("/add")
    public Result<Long> add(@RequestBody
                            @Validated ExamAddDTO examAddDTO){
        return Result.ok(examService.add(examAddDTO));
    }
    /**
     * 新增竞赛有题目,具体执行逻辑就是先新增一个没有题目的竞赛,根据竞赛id去添加题目
     * @param examAddQuestionDTO
     * @return
     */
    @PostMapping("/question/add")
    public Result<Void> addQuestion(@RequestBody ExamAddQuestionDTO examAddQuestionDTO){
        log.info("addQuestion ExamAddQuestionDTO接收到的参数为:{}",examAddQuestionDTO);
        return toResult(examService.addQuestion(examAddQuestionDTO));
    }

    /**
     * 获取竞赛详情
     */
    @GetMapping("/detail")
    public Result<ExamDetailVO> detail(Long examId){
        return Result.ok(examService.detail(examId));
    }

    /**
     * 编辑竞赛
     */
    @PutMapping("/edit")
    public Result<Void> edit(ExamEditDTO examEditDTO){
        return toResult(examService.edit(examEditDTO));
    }

    /**
     * 删除竞赛中的题目
     */
    @DeleteMapping("/question/delete")
    public Result<Void> delete(Long examId,Long questionId){
        return toResult(examService.delete(examId,questionId));
    }


    /**
     * 删除竞赛
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteExam(Long examId){
        return toResult(examService.deleteExam(examId));
    }

    /**
     * 发布竞赛
     */
    @PutMapping("/publish")
    public Result<Void> publish(Long examId){
        return toResult(examService.publish(examId));
    }

    /**
     * 撤销发布竞赛
     */
    @PutMapping("/cancelPublish")
    public Result<Void> cancelPublish(Long examId){
        log.info("撤销发布竞赛的id为:{}",examId);
        return toResult(examService.cancelPublish(examId));
    }
}
